<template>
	<view class="uTabbar">
		<view class="u-demo-block">
			<u-row justify="space-between">
				<u-col span="12" v-if="token">
					<view class="userinfo">
						<u-avatar :src="userInfo.avatar" size="90rpx"></u-avatar>
						<view class="userbox">
							<u--text :text="userInfo.nickname" lines="1" bold size="36rpx"></u--text>
							<u--text :text="userInfo.signature || '这家伙什么也没留下~'" lines="1" size="24rpx" color="#999"
								:customStyle="{'margin-top':'20rpx','line-height':'24rpx'}"></u--text>
						</view>
					</view>
				</u-col>
				<u-col span="12" v-else>
					<view class="userinfo" @click="turnTo('/pages/login/index')">
						<u-avatar :src="logoImg" size="124rpx"></u-avatar>
						<u--text text="未登录" size="36rpx" bold :customStyle="{'margin-left':'20rpx'}"></u--text>
					</view>
				</u-col>
			</u-row>
		</view>
		<!-- 热门活动 -->
		<view class="promotion">
			<u-transition :show="true" mode="slide-up">
				<view class="list">
					<view class="column" @click="turnTo('/pages/user/signin')">
						<view class="top">
							<view class="title">每日签到</view>
						</view>
						<view class="left">
							<view class="ad"></view>
							<view class="into">领取免费额度</view>
						</view>
						<view class="right">
							<image :src="assetUrl + 'qiandao.png'"></image>
						</view>
					</view>
					<!-- 登录状态下，可触发分享按钮 -->
					<view class="column" v-if="token">
						<button class="share-btn" open-type="share"></button>
						<view class="top">
							<view class="title">邀请好友</view>
						</view>
						<view class="left">
							<view class="ad"></view>
							<view class="into">邀请得额度</view>
						</view>
						<view class="right">
							<image :src="assetUrl + 'share.png'"></image>
						</view>
					</view>
					<!-- 未登录状态下，触发提示 -->
					<view class="column" v-if="!token" @click="shareTips">
						<view class="top">
							<view class="title">邀请好友</view>
						</view>
						<view class="left">
							<view class="ad"></view>
							<view class="into">邀请有礼</view>
						</view>
						<view class="right">
							<image :src="assetUrl + 'share.png'"></image>
						</view>
					</view>
				</view>
			</u-transition>
		</view>
		<u-transition :show="true" mode="fade-left">
			<view class="quickly-enter padding-box">
				<u-grid :border="false" col="4">
					<u-grid-item v-for="(listItem,listIndex) in list" :key="listIndex" @click="turnTo(listItem.url)"
						bgColor="#fff" :customStyle="{'margin-bottom':'60rpx'}">
						<u-icon :name="listItem.name" width="50rpx" height="48rpx"></u-icon>
						<u--text :text="listItem.title" align="center" size="24rpx" lineHeight="24rpx"
							:customStyle="{'margin-top':'20rpx'}"></u--text>
					</u-grid-item>
				</u-grid>
			</view>
		</u-transition>
		<u-transition :show="true" mode="slide-right">
			<view class="quickly-enter">
				<u-cell-group :border="false">
					<u-cell v-if="token" :icon="assetUrl + 'my_09.png'" title="退出登录" :border="false"
						iconStyle="width:50rpx;height:48rpx" customStyle="padding:8rpx 20rpx;" @click="logout()">
					</u-cell>
				</u-cell-group>
			</view>
		</u-transition>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: uni.getStorageSync('userInfo'),
				token: uni.getStorageSync('token'),
				logoImg: this.$configData.assetsUrl + 'logo.png',
				list: [{
					name: this.$configData.assetsUrl + 'my_source.png',
					title: '识别记录',
					url: '/pages/user/mySource'
				}, {
					name: this.$configData.assetsUrl + 'suggest.png',
					title: '意见反馈',
					url: '/pages/feedback/feedback'
				}, {
					name: this.$configData.assetsUrl + 'my_08.png',
					title: '个人资料',
					url: '/pages/user/info'
				}],
				assetUrl: this.$configData.assetsUrl
			}
		},
		onShareAppMessage(res) {
			return {
				title: '爱酷AI，带你体验最前沿的人工智能',
				path: '/pages/index/index?inviter=' + encodeURIComponent(this.userInfo.encode_id)
			}
		},
		onShareTimeline(res) {
			return {
				title: '爱酷AI，带你体验最前沿的人工智能',
				path: '/pages/index/index?inviter=' + encodeURIComponent(this.userInfo.encode_id)
			}
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo');
		},
		methods: {
			shareTips() {
				var that = this;
				//用户未登录，提示用户登录后分享邀请
				uni.showModal({
					title: '温馨提示',
					content: '您尚未登录，邀请的好友无法记绑定到您名下，是否立即登录',
					success: function(res) {
						if (res.confirm) {
							that.turnTo('/pages/login/index');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			turnTo(url) {
				if (!url) {
					uni.showToast({ //提示
						title: "敬请期待",
						icon: "none"
					});
					return false;
				}
				uni.navigateTo({
					url: url
				});
			},
			logout() {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '确定要退出吗?',
					success(res) {
						if (res.confirm) {
							_this.$store.commit('logout');
							_this.userinfo = {};
							uni.reLaunch({
								url: '/pages/index/index',
							});
						}
					}
				});
			}
		},
	}
</script>

<style lang="scss">
	.u-demo-block {
		padding: 60rpx 30rpx;
		margin-bottom: 0;
	}

	.userinfo {
		@include flex;
		align-items: center;

		.userbox {
			margin-left: 20rpx;
		}
	}

	.quickly-enter {
		width: 92%;
		border-radius: 14rpx;
		margin: 20rpx auto;
		background-color: #fff;
		margin-top: 20rpx;
		overflow: hidden;
		box-shadow: 0rpx 0rpx 25rpx rgba(0, 0, 0, 0.05);

		&.padding-box {
			padding-top: 40rpx;
		}
	}

	.promotion {
		width: 92%;
		margin: 0 4%;

		.list {
			width: 100%;
			display: flex;
			justify-content: space-between;

			.column {
				width: 43%;
				padding: 26rpx 3%;
				background-color: #ebf9f9;
				border-radius: 10rpx;
				overflow: hidden;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				box-shadow: 0rpx 0rpx 25rpx rgba(0, 0, 0, 0.05);
				position: relative;

				.share-btn {
					width: 100%;
					height: 100%;
					opacity: 0;
					position: absolute;
					top: 0;
					left: 0;
				}

				.top {
					width: 100%;
					height: 40rpx;
					display: flex;
					align-items: center;
					margin-bottom: 5rpx;

					.title {
						font-size: 30rpx;
					}

					.countdown {
						margin-left: 20rpx;
						display: flex;
						height: 40rpx;
						display: flex;
						align-items: center;
						font-size: 20rpx;

						view {
							height: 30rpx;
							background-color: #f06c7a;
							display: flex;
							justify-content: center;
							align-items: center;
							color: #fff;
							border-radius: 4rpx;
							margin: 0 4rpx;
							padding: 0 2rpx;
						}
					}
				}

				.left {
					width: 50%;
					// height: 120rpx;
					display: flex;
					flex-wrap: wrap;
					align-content: space-between;

					.ad {
						margin-top: 5rpx;
						width: 100%;
						font-size: 22rpx;
						color: #acb0b0;
					}

					.into {
						width: 100%;
						font-size: 24rpx;
						color: #aaa;
						margin-bottom: 5rpx;
					}
				}

				.right {
					width: 80rpx;
					height: 80rpx;

					image {
						width: 80rpx;
						height: 80rpx;
					}
				}
			}
		}
	}
</style>